package com.uzhie.web.controller.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uzhie.exception.BaseDataException;
import com.uzhie.web.anno.Auth;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.controller.http.comm.CommController;
import com.uzhie.web.module.BaseData;
import com.uzhie.web.service.BaseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 字典信息请求类
 * Created by lilongfei on 2018/7/20.
 */
@Controller
@RequestMapping(value = "baseData")
public class BaseDataController extends CommController {

    @Autowired
    BaseDataService baseDataService;

    /**
     * 查询字典列表
     *
     * @param body     type 查询字典的类型
     *                 status 查询字典的状态 -1表示全部,1表示启用,2表示禁用
     * @param pageBean
     * @return
     */
    @Auth
    @ResponseBody
    @RequestMapping(value = "queryList")
    public PayloadBean queryList(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean pageBean) {
        try {
            List<BaseData> page = baseDataService.queryList(body.getString("type"), body.getIntValue("status"), pageBean.getRowBounds());
            return buildResponseWithPage(page, pageBean);
        } catch (BaseDataException | IllegalArgumentException e) {
            return buildResponse(null, "查询失败" + e.getMessage());
        }
    }

    /**
     * 查询字典列表
     *
     * @param body     types 查询字典的类型集合
     *                 status 查询字典的状态 -1表示全部,1表示启用,2表示禁用
     * @param body
     * @param pageBean
     * @return
     */
    @Auth
    @ResponseBody
    @RequestMapping(value = "queryListByTypes")
    public PayloadBean queryListByTypes(@RequestBody(required = false) JSONObject body, @RequestBody(required = false) PageBean pageBean) {
        try {
            List<String> categorys = JSONArray.parseArray(body.getJSONArray("types").toJSONString(), String.class);
            Map<String, List<BaseData>> page = baseDataService.queryListByTypes(categorys, body.getInteger("status"), pageBean.getRowBounds());
            return buildResponse(page);
        } catch (BaseDataException | IllegalArgumentException e) {
            return buildResponse(null, "查询失败" + e.getMessage());
        }
    }


    /**
     * 添加字典对象
     *
     * @param body 字典对象
     * @return
     */
    @Auth
    @ResponseBody
    @RequestMapping(value = "addBaseData")
    public PayloadBean addBaseData(@RequestBody(required = false) JSONObject body) {
        try {
            baseDataService.addBaseData(JSON.toJavaObject(body, BaseData.class));
            return buildResponse(null);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, "添加字典失败" + e.getMessage());
        }
    }

    /**
     * 更新字典数据
     *
     * @param body
     * @return
     */
    @Auth
    @ResponseBody
    @RequestMapping(value = "updateBaseData")
    public PayloadBean updateBaseData(@RequestBody(required = false) JSONObject body) {
        try {
            baseDataService.updateBaseData(JSON.toJavaObject(body, BaseData.class));
            return buildResponse(null);
        } catch (IllegalArgumentException e) {
            return buildResponse(null, "更新字典失败" + e.getMessage());
        }
    }

    /**
     * 删除字典数据
     *
     * @param body
     * @return
     */
    @Auth
    @ResponseBody
    @RequestMapping(value = "deleteBaseData")
    public PayloadBean deleteBaseData(@RequestBody(required = false) JSONObject body) {
        try {
            baseDataService.deleteBaseData(JSON.toJavaObject(body.getJSONObject("baseData"), BaseData.class));
            return buildResponse(null);
        } catch (IllegalArgumentException | BaseDataException e) {
            return buildResponse(null, "删除字典失败" + e.getMessage());
        }
    }

    /**
     * 更新字典数据状态
     *
     * @param body baseData 字典数据内容
     *             status 数据状态
     * @return
     */
    @Auth
    @ResponseBody
    @RequestMapping(value = "updateBaseDateStatue")
    public PayloadBean updateBaseDateStatue(@RequestBody(required = false) JSONObject body) {
        try {
            baseDataService.updateStatus
                    (JSON.toJavaObject(body.getJSONObject("baseData"), BaseData.class), body.getInteger("status"));
            return buildResponse(null);
        } catch (IllegalArgumentException | BaseDataException e) {
            return buildResponse(null, "更新字典状态失败:" + e.getMessage());
        }
    }

}
